<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="开始时间" prop="createStartTime">
        <el-date-picker
          v-model="queryParams.createStartTime"
          type="date"
          placeholder="请选择开始时间"
          clearable
          :editable="false"
          value-format="yyyy-MM-dd"
          @keyup.enter.native="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="createEndTime">
        <el-date-picker
          v-model="queryParams.createEndTime"
          type="date"
          placeholder="请选择结束时间"
          clearable
          :editable="false"
          value-format="yyyy-MM-dd"
          @keyup.enter.native="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item prop="timeFrame">
        <el-button-group>
          <el-button
            :type="queryParams.timeFrame == 7 ? 'primary' : ''"
            @click="handleTimeFrame(7)"
            >7天</el-button
          >
          <el-button
            :type="queryParams.timeFrame == 30 ? 'primary' : ''"
            @click="handleTimeFrame(30)"
            >30天</el-button
          >
        </el-button-group>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <div class="volume-wrap">
      <div class="volume-box">
        <div>总成交</div>
        <el-divider></el-divider>
        <div class="volume-data">
          <div class="volume-data-item">
            <div>{{ countMarketList.orderNumber }}</div>
            <div class="data-item-name">付款订单数/个</div>
          </div>
          <div class="volume-data-item">
            <div>{{ countMarketList.productNumber }}</div>

            <div class="data-item-name">付款件数/件</div>
          </div>
          <div class="volume-data-item">
            <div>{{ countMarketList.userNumber }}</div>

            <div class="data-item-name">付款人数/人</div>
          </div>
          <div class="volume-data-item">
            <div>{{ countMarketList.money }}</div>

            <div class="data-item-name">付款金额/元</div>
          </div>
        </div>
      </div>
      <div class="volume-box">
        <div>今日实时成交</div>
        <el-divider></el-divider>
        <div class="volume-data">
          <div class="volume-data-item">
            <div>{{ todayMarketList.orderNumber }}</div>
            <div class="data-item-name">付款订单数/个</div>
          </div>
          <div class="volume-data-item">
            <div>{{ todayMarketList.productNumber }}</div>

            <div class="data-item-name">付款件数/件</div>
          </div>
          <div class="volume-data-item">
            <div>{{ todayMarketList.userNumber }}</div>

            <div class="data-item-name">付款人数/人</div>
          </div>
          <div class="volume-data-item">
            <div>{{ todayMarketList.money }}</div>

            <div class="data-item-name">付款金额/元</div>
          </div>
        </div>
      </div>
    </div>

    <!-- v-loading="loading" -->
    <el-table
      :data="selectMarketOrderList"
      highlight-current-row
      max-height="1000"
    >
      <el-table-column label="日期" align="center" prop="time" />
      <el-table-column label="付款人数" align="center" prop="userNumber" />
      <el-table-column label="付款订单数" align="center" prop="orderNumber" />
      <el-table-column label="付款金额" align="center" prop="money" />
      <el-table-column label="付款件数" align="center" prop="productNumber" />
    </el-table>

    <!-- total -->
    <pagination
      v-show="selectMarketOrderList.length > 0"
      :total="selectMarketOrderList.length"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  countMarketData,
  todayMarketData,
  selectMarketOrderData,
} from "@/api/data/sales";
export default {
  name: "Sales",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createStartTime: null,
        createEndTime: null,
        timeFrame: null,
      },
      countMarketList: {},
      todayMarketList: {},
      // 表格数据
      selectMarketOrderList: [],
    };
  },
  created() {},
  mounted() {
    this.getCountMarketData();
    this.getTodayMarketData();
    this.selectMarketOrderData();
  },
  methods: {
    // 查询销售数据列表
    getList() {
      this.loading = false;
      // api
    },
    /**销售数据 总成交 */
    getCountMarketData() {
      let params = {
        createStartTime: this.queryParams.createStartTime,
        createEndTime: this.queryParams.createEndTime,
      };
      countMarketData(params).then((res) => {
        this.countMarketList = res.data;
      });
    },
    /**销售数据 当日成交 */
    getTodayMarketData() {
      let today = new Date().toISOString().slice(0, 10);
      let params = {
        createStartTime: today + " 00:00:00",
        createEndTime: today + " 23:59:59",
      };
      todayMarketData(params).then((res) => {
        this.todayMarketList = res.data;
      });
    },
    /**销售数据成交订单时间列表 */
    selectMarketOrderData() {
      selectMarketOrderData(this.queryParams).then((res) => {
        this.selectMarketOrderList = res.rows;
      });
    },
    // 时间范围按钮
    handleTimeFrame(dayNum) {
      let date = Date.now();
      this.queryParams.timeFrame = dayNum;
      if (dayNum == 7) {
        this.queryParams.createEndTime = this.$dayjs(date).format("YYYY-MM-DD");
        this.queryParams.createStartTime = this.$dayjs(
          new Date(date - 1000 * 60 * 60 * 24 * 7)
        ).format("YYYY-MM-DD");
      }
      if (dayNum == 30) {
        this.queryParams.createEndTime = this.$dayjs(date).format("YYYY-MM-DD");
        this.queryParams.createStartTime = this.$dayjs(
          new Date(date - 1000 * 60 * 60 * 24 * 30)
        ).format("YYYY-MM-DD");
      }
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getCountMarketData();
      this.getTodayMarketData();
      this.selectMarketOrderData();
    },
    // 重置按钮
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>

<style lang="less" scoped>
.volume-wrap {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 0 40px;

  .volume-box {
    width: 48%;

    .volume-data {
      display: flex;

      .volume-data-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-content: center;
        border-left: 1px dashed #eff1f7;

        div {
          display: flex;
          justify-content: center;
          align-items: center;
          font-size: 28px;
          color: #303133;
        }

        .data-item-name {
          margin: 6px 0 0;
          color: #92959b;
          font-size: 16px;
        }
      }

      .volume-data-item:first-child {
        border: none;
      }
    }
  }
}
</style>
